﻿package Kerjin.FusionTablesClient;

import java.util.Hashtable;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import Kerjin.Util.Google.FusionTablesClient;

public class Main extends Activity
{
	private Button btnStart;
	private TextView labMessage;
	
	public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        btnStart = (Button)findViewById(R.id.btn_start);
        labMessage = (TextView)findViewById(R.id.lab_message);
        
        btnStart.setOnClickListener(btnStart_OnClickListener);
    }
	
	private Button.OnClickListener btnStart_OnClickListener = new Button.OnClickListener()
	{
		public void onClick(View v) 
    	{
			StringBuffer sbMessage = new StringBuffer();
			
			FusionTablesClient ftc = new FusionTablesClient();
			if (ftc.GetAuth("{請在此輸入帳號, ex. google.yu@gmail.com}", "請在此輸入密碼"))
			{
				sbMessage.append(ftc.getMessage() + "\n");
				// 資料表欄名與資料型態
				// 請自行參考 https://developers.google.com/fusiontables/docs/developers_guide?hl=zh-tw#CreatingTable
				Hashtable htabFieldInfo = new Hashtable();
				htabFieldInfo.put("FILE_BLOCK_ID", "NUMBER");
				htabFieldInfo.put("FILE_NAME", "STRING");
				htabFieldInfo.put("FILE_NAME_EXT", "STRING");
				htabFieldInfo.put("FILE_URL", "STRING");
				// 建立資料表
				String strTableID = ftc.CreateTable("test", htabFieldInfo);
				sbMessage.append("Create Table => " + ftc.getMessage() + "\n");
				
				if (strTableID != "")
				{
					// 取得資料表機碼資訊
					sbMessage.append("Table Shema: \n");
					sbMessage.append(ftc.GetTableShema(strTableID) + "\n");
					sbMessage.append("Get Table Shema => " + ftc.getMessage() + "\n");
					
					// 新增資料列(執行一個SQL敘述)
					sbMessage.append(ftc.ExecuteQuery("INSERT INTO " + strTableID + " ('FILE_BLOCK_ID','FILE_NAME','FILE_NAME_EXT','FILE_URL') VALUES (1,'test','.txt','http://127.0.0.1/text.txt')") + "\n");
					sbMessage.append("Insert => " + ftc.getMessage() + "\n");
					
					// 取得資料
					sbMessage.append(ftc.ExecuteQuery("SELECT * FROM " + strTableID) + "\n");
					sbMessage.append("Get Data = >" + ftc.getMessage() + "\n");
					
					// 刪除資料表
					if (ftc.DropTable(strTableID))
					{
						sbMessage.append("Drop Table => " + ftc.getMessage() + "\n");
					}
					else
					{
						sbMessage.append(ftc.getMessage());
					}
				}
			}
			else
			{
				sbMessage.append("無法取得認證。\n");
			}
			labMessage.setText(sbMessage.toString());
    	}
	};
}
